package com.coin900.coin.server.coin.ctrl;

import com.coin900.coin.base.AjaxResponse;
import com.coin900.coin.base.CoinConstants;
import com.coin900.coin.persist.coin.CoinCoinVO;
import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.persist.fu.FuUserWithdrawalAddressVO;
import com.coin900.coin.server.BaseController;
import com.coin900.coin.service.coin.ICoinCoinService;
import com.coin900.coin.service.fu.IFuUserWithdrawalAddressService;
import com.value.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户地址管理
 *
 * @author shui
 * @create 2018-01-26
 **/
@Controller
public class CoinUserAddressCtrl extends BaseController {

    /**
     * 用户提现地址管理界面
     *
     * @param viewType
     * @param request
     * @return
     */
    @RequestMapping(value = "/withdraw-address", method = RequestMethod.GET)
    public ModelAndView address(@RequestParam String viewType,
                                HttpServletRequest request) {
        if (CoinConstants.COIN_PAGE.equals(viewType)) {
            ModelAndView modelAndView = buildView(request, "/project/add-address");
            // 币种列表
            Map<String, Object> params = new HashMap<>();
            params.put("dataStatus", 1);
            params.put("showInWalletStatus", 1);
            List<CoinCoinVO> coinCoinVOS = this.coinCoinService.loadCoinCoin(params);
            if (!coinCoinVOS.isEmpty()) {
                modelAndView.addObject("coinList", coinCoinVOS);
            }
            // 地址列表
            params.clear();
            params.put("dataStatus", 1);
            params.put("userId", super.getUser().getId());
            List<FuUserWithdrawalAddressVO> fuUserWithdrawalAddressVOS = this.addressService.loadFuUserWithdrawalAddress(params);
            if (!fuUserWithdrawalAddressVOS.isEmpty()) {
                modelAndView.addObject("addressList", fuUserWithdrawalAddressVOS);
            }
            return modelAndView;
        }

        return null;
    }

    /**
     * 新增用户地址
     *
     * @param addressVO
     * @param request
     * @return
     */
    @RequestMapping(value = "/withdraw-address", method = RequestMethod.POST)
    public ModelAndView add(@RequestBody FuUserWithdrawalAddressVO addressVO,
                            HttpServletRequest request) {
        AjaxResponse response = super.buildAjaxResponse(request);
        FuUserVO user = super.getUser();
        addressVO.setUserId(user.getId());
        addressVO.setCreatedInfor(user);
        this.addressService.insertFuUserWithdrawalAddress(addressVO);
        return response.getMav();
    }

    /**
     * 删除用户充值地址
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping(value = "/withdraw-address", method = RequestMethod.DELETE)
    public ModelAndView delete(@RequestParam Long id,
                               HttpServletRequest request) {
        AjaxResponse response = super.buildAjaxResponse(request);
        FuUserVO user = super.getUser();
        Map<String, Object> params = new HashMap<>();
        params.put("id", id);
        params.put("dataStatus", 1);
        List<FuUserWithdrawalAddressVO> fuUserWithdrawalAddressVOS = this.addressService.loadFuUserWithdrawalAddress(params);
        if (fuUserWithdrawalAddressVOS.isEmpty()) {
            throw new BusinessException("没有该提现地址记录");
        }

        FuUserWithdrawalAddressVO fuUserWithdrawalAddressVO = fuUserWithdrawalAddressVOS.get(0);
        fuUserWithdrawalAddressVO.setModifiedInfor(user);
        fuUserWithdrawalAddressVO.setDataStatus(0);
        this.addressService.deleteFuUserWithdrawalAddress(fuUserWithdrawalAddressVO);
        return response.getMav();
    }

    /**
     * 获取某种币的地址列表
     *
     * @param coinId
     * @param request
     * @return
     */
    @RequestMapping(value = "/address-list", method = RequestMethod.GET)
    public Object addressList(@RequestParam Long coinId,
                              HttpServletRequest request) {
        FuUserVO user = super.getUser();
        Map<String, Object> params = new HashMap<>();
        params.put("userId", user.getId());
        params.put("coinId", coinId);
        params.put("dataStatus", 1);
        List<FuUserWithdrawalAddressVO> addressList = this.addressService.loadFuUserWithdrawalAddress(params);
        if(!addressList.isEmpty()){
            return addressList;
        }
        return null;
    }

    @Autowired
    private IFuUserWithdrawalAddressService addressService;
    @Autowired
    private ICoinCoinService coinCoinService;

}
